<?php
namespace Admin\Controller;
class ProjectController extends CommonController {
	
	 /*
	 *  列表
	 *
	 */
	 public function index(){
	    
		$act=I('get.act');
       
        if (!empty($act)&&$act==='search') {
		   $keywords=trim(urldecode(I('keywords')));	
           $condition=array();
           $con=array();
           if(!empty($keywords)){
			 $condition['pname']=array('like','%'.$keywords.'%');
		   
           }
         $con['keywords']=$keywords;
        }
       
       
        $count=M('project')->where($condition)->count();
		
        $p=getpage($count,9);
		 
		 foreach($con as $key=>$val) 
		 {
           $p->parameter[$key]   = urlencode($val);
         } 
		 
         $project_arr=M('project')->where($condition)->order('pid desc')->limit($p->firstRow.','.$p->listRows)->select();
	  
		 
         $this->project=$project_arr;
		 
		 $this->assign('page',$p->show());
	 
         $this->assign('con',$con);
		 
		 $this->display();
	 }
	 
	
	

   /*
	*
	*  添加+修改
	*
	*
	*/
	public function add(){
		 
	    $act=I('act','');
		
		$id=I('id',0,'intval');
		 

		if($act=='add')
		{   
	        
			$pname=I('post.pname');
			$opt=I('post.opt');
			$point=I('post.point');
		 
			!is_empty($pname,$opt,$point)?message(2,L('_IS_EMPTY_')):''; 
		 
            $db_arr=array(
			  'pname'=>$pname,
			  'opt'=>$opt,
			  'point'=>$point,
			  
			); 
			
		 
			if(!empty($id)){
			   
			    $lastId=M('project')->where(array('pid'=>$id))->save($db_arr);
			 
			}else{
			    
				$lastId=M('project')->add($db_arr);
			 
			}
	  
			!empty($lastId)?message(1,L('_DO_SUCCEED_'),U('Project/index')):message(2,L('_DO_FAILED_'));
			
		}

	    $edit=!empty($id)?M('project')->where(array('pid'=>$id))->find():'';
		
		$this->edit=$edit;
 
		$this->display(); 
	}	
	
	 
	 
	  /*
	*
	* 删除
	*
	*/
	public function del(){
		
        $id=I('post.id',0);

        if(!empty($id)){
			
			$id_arr=is_array($id)?$id:array($id);
		   	
			foreach($id_arr as $k=>$v){
		 
				  
				$delId=M('project')->where(array('pid'=>$v))->delete();
				
				if(empty($delId)) continue;
	  
			}
			
			message(1,L('_DO_SUCCEED_'),U('Project/index'));
		}		
		message(2,L('_DO_FAILED_'));
	}
	 
	 
}

 